package com.groupbuying.storeinternal.service.impl;

import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import com.groupbuying.storeinternal.connector.SysconfigFeign;
import com.groupbuying.storeinternal.constants.StoreCustomerCollectEnumConstants;
import com.groupbuying.storeinternal.utils.Query;
import com.groupbuying.storeinternal.utils.R;
import com.groupbuying.storeinternal.utils.RedisUtils;
import com.groupbuying.storeinternal.utils.apiresult.ApiResult;
import com.groupbuying.storeinternal.utils.apiresult.AppApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.groupbuying.storeinternal.dao.StoreCustomerCollectDao;
import com.groupbuying.storeinternal.domain.StoreCustomerCollectDO;
import com.groupbuying.storeinternal.service.StoreCustomerCollectService;



@Service
public class StoreCustomerCollectServiceImpl implements StoreCustomerCollectService {
	@Autowired
	private StoreCustomerCollectDao storeCustomerCollectDao;

	@Autowired
	private SysconfigFeign sysconfigFeign;

	@Autowired
	private RedisUtils redisUtils;

	
	@Override
	public StoreCustomerCollectDO get(String id){
		return storeCustomerCollectDao.get(id);
	}
	
	@Override
	public List<StoreCustomerCollectDO> list(Map<String, Object> map){
		return storeCustomerCollectDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return storeCustomerCollectDao.count(map);
	}
	
	@Override
	public int save(StoreCustomerCollectDO storeCustomerCollect){
		return storeCustomerCollectDao.save(storeCustomerCollect);
	}
	
	@Override
	public int update(StoreCustomerCollectDO storeCustomerCollect){
		return storeCustomerCollectDao.update(storeCustomerCollect);
	}
	
	@Override
	public int remove(String id){
		return storeCustomerCollectDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return storeCustomerCollectDao.batchRemove(ids);
	}

	@Override
	public Object saveOrDeleteCollect(Map<String, Object> params) {

		Object temp = params.get("status");
		String status = temp == null ? "" : temp.toString();
		temp = params.get("customerUuid");
		String customerUuid = temp == null ? "" : temp.toString();
		//不再校验code
		temp = params.get("storeUuid");
		String storeUuid = temp == null ? "" : temp.toString();

		if (StringUtils.isBlank(status) || StringUtils.isBlank(customerUuid) || StringUtils.isBlank(storeUuid)) {
			return AppApiResult.failure("param is null");
		}
		Map queryParam = new HashMap();
		queryParam.put("customerUuid", customerUuid);
		queryParam.put("storeUuid", storeUuid);
		List<StoreCustomerCollectDO> storeCustomerCollectDOList = storeCustomerCollectDao.list(queryParam);
		StoreCustomerCollectDO storeCustomerCollectDO = storeCustomerCollectDOList != null && storeCustomerCollectDOList.size() > 0 ? storeCustomerCollectDOList.get(0) : null;
		if(storeCustomerCollectDO!=null){
			storeCustomerCollectDO.setIsDelete(status);
			storeCustomerCollectDao.update(storeCustomerCollectDO);
			return AppApiResult.success();
		}
		storeCustomerCollectDO = new StoreCustomerCollectDO();
		storeCustomerCollectDO.setIsDelete(status);
		storeCustomerCollectDO.setCustomerUuid(customerUuid);
		storeCustomerCollectDO.setStoreUuid(storeUuid);
		storeCustomerCollectDO.setId(sysconfigFeign.getUuidByLeaf());
		storeCustomerCollectDao.save(storeCustomerCollectDO);

		return AppApiResult.success();
 	}


}
